package com.kun.index.controller;


import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.common.annotation.PermissionAuthentication;
import com.kun.index.entity.*;
import com.kun.index.service.impl.BlogServiceImpl;
import com.kun.index.service.impl.CommentServiceImpl;
import com.kun.index.service.impl.MessageServiceImpl;
import com.kun.index.service.impl.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消息通知表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/index/message")
public class MessageController {

    @Autowired
    private MessageServiceImpl messageService;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private CommentServiceImpl commentService;

    @Autowired
    private BlogServiceImpl blogService;


    @GetMapping("/clearRedPot")
    @ApiOperation("给消息消除红点")
    public ResultDto clearRedPot(@RequestParam(name = "type", required=false)String type,
                                @RequestParam(name = "userId", required=false)Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        if (type != null) {
            map.put("type", type);
        }
        if (userId != null) {
            map.put("userId", userId);
        }
        Map<String, Object> queryMessages = messageService.queryMessages(map);
        List<Message> messages= (List<Message>) queryMessages.get("messages");
        int num=0;
        for (Message message : messages) {
            message.setIsNew(0);
            num+= messageService.updateMessage(message);
        }
        if (num>0){
            return ResultDto.success("给用户发送消息成功");
        }else{
            return ResultDto.fail("给用户发送消息失败");
        }
    }

    @PostMapping("/addMessage")
    @ApiOperation("给用户发送消息")
    public ResultDto addMessage(@RequestBody Message message) {

        Integer num = messageService.addMessage(message);
        if (num!=null){
            return ResultDto.success("给用户发送消息成功");
        }else{
            return ResultDto.fail("给用户发送消息失败");
        }
    }

    @GetMapping("/queryAllMessages")
    @ApiOperation("查询所有的消息")
    public ResultDto queryAllMessages(@RequestParam(name = "pageIndex", required=false)Integer pageIndex,
                                      @RequestParam(name = "userId", required=false)Integer userId,
                                      @RequestParam(name = "orderByCreateTime", required=false)String orderByCreateTime,
                                      @RequestParam(name = "pageSize", required=false)Integer pageSize) {
        HashMap<String, Object> map = new HashMap<>();
        if (pageIndex != null) {
            map.put("pageIndex", pageIndex);
        }
        if (userId != null) {
            map.put("userId", userId);
        }
        if (pageSize != null) {
            map.put("pageSize", pageSize);
        }
        if (orderByCreateTime != null) {
            map.put("orderByCreateTime", orderByCreateTime);
        }

        Map<String, Object> resultMap = messageService.queryMessages(map);
        List<Message> messages = (List<Message>) resultMap.get("messages");
        if (messages.size() > 0) {

            for (Message message : messages) {

//                注入通知者的头像url
                User user = userService.queryUserById(message.getByUserId());
                message.setByUserAvatarUrl(user.getAvatarUrl());
//                注入通知者的昵称
                message.setByUserNickname(user.getNickname());
//                注入通知者的身份
                message.setByUserRole((user.getRole()==1)?Constants.ROLE_1:Constants.ROLE_0);

//                注入通知来源内容
                if (message.getType().equals(Constants.MESSAGE_COMMENT)){
                    message.setTypeContent(commentService.queryCommentById(message.getTypeId()).getContent());
                }else if (message.getType().equals(Constants.MESSAGE_BLOG)){
                    message.setTypeContent(blogService.queryBlogById(message.getTypeId()).getTitle());
                }
            }
            return ResultDto.success(resultMap);
        } else {
            return ResultDto.success("您暂时未有消息通知！");
        }
    }

}

